﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.EntityFrameworkCore;
using OABackground.Entities;

namespace OABackground.Infrastructure
{
    public class ConventionApplyRepository
    {
        #region 会议申请增删改查
        public void Add(Conventionapply convention)//添加一个会议申请
        {

            using (var dbcontext = new oaContext())
            {

                dbcontext.Conventionapply.Add(convention);//这不是真的添加
                dbcontext.SaveChanges();//用“保存”表示一次事务的完成
            }
        }
        public void Update(Conventionapply convention)//更新会议申请
        {
            using (var dbcontext = new oaContext())
            {
                dbcontext.Update(convention);
                dbcontext.SaveChanges();
            }
        }
        public Conventionapply Query(string place)//根据会议地点查找会议申请
        {

            using (var dbcontext = new oaContext())
            {
                var select = dbcontext.Conventionapply.Find(place);
                dbcontext.SaveChanges();

                return select;
            }
        }
        public List<Conventionapply> QueryByTeacher (string Tid)//根据教师id查找会议
        {
            List<Conventionapply> conventionlist = new List<Conventionapply>();

            using (var dbcontext = new oaContext())
            {
                conventionlist = dbcontext.Conventionapply.
                    FromSql($"select * from Conventionapply where Contact={Tid}").ToList();
                dbcontext.SaveChanges();

                return conventionlist;
            }
        }
        public void Delete(int id)//删除会议申请
        {
            using (var dbcontext = new oaContext())
            {
                var select = dbcontext.Teacherinfomation.Find(id);
                dbcontext.Remove(select);
                dbcontext.SaveChanges();
            }

        }
        public bool QueryByStarttime(DateTime time)//根据会议地点查找会议申请
        {
            using (var dbcontext = new oaContext())
            {
                var select = dbcontext.Conventionapply.FirstOrDefault(x => x.StartTime == time);
                dbcontext.SaveChanges();
                if (select == null)
                    return true;
                else
                    return false;
            }
        }
        public bool QueryByEndtime(DateTime time)//根据会议地点查找会议申请
        {
            using (var dbcontext = new oaContext())
            {
                var select = dbcontext.Conventionapply.FirstOrDefault(x => x.EndTime == time);
                dbcontext.SaveChanges();
                if (select == null)
                    return true;
                else
                    return false;
            }
        }
        #endregion
    }
}
